package com.demoo.zjsu.controller.testnumber;

import com.demoo.zjsu.dao.testnumber.TestNumberDAO;
import com.demoo.zjsu.model.testnumber.Note;
import com.demoo.zjsu.model.userinfo.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by jshjsh on 2016/8/21.
 */
@Controller
public class TestContrroller {

    @Autowired
    TestNumberDAO dao;


    @RequestMapping(value = "testNumber/get",method = RequestMethod.GET)
    public ModelAndView getTestNumber(HttpSession session) {
        UserInfo user = (UserInfo) session.getAttribute("user");
        int id = user.getId();
        List<Note> notes = dao.getListNotesByUserId(id);
        return new ModelAndView("testNumber").addObject("note", notes);
    }


    @RequestMapping(value = "testNumber/form/add",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,String>  addNote(@ModelAttribute(value = "note") Note note, HttpSession session) {
        UserInfo user = (UserInfo) session.getAttribute("user");
        int id = user.getId();
        note.setUserID(id);
        Map<String, String> map = new HashMap<String, String>();
        try {
            map.put("data", dao.addNote(note)+"");
            map.put("id",note.getId());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }


    @ResponseBody
    @RequestMapping(value = "testNumber/form/delete",method = RequestMethod.POST)
    public boolean deleteNote(@RequestBody Note note) {
        return dao.deleteNote(note);
    }
}
